package com.tianhui.consignortwofei.mvp.ui.activity.audit.vehicle;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.fgs.common.CommonResponse;
import com.fgs.common.entity.CommonDataListInfo;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.widget.SelectDialog;
import com.fgs.common.widget.itemView.ClickItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.mvp.model.VehicleModel;
import com.tianhui.consignortwofei.mvp.model.enty.DictionaryInfo;
import com.tianhui.consignortwofei.mvp.model.enty.VehicleInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.audit.BaseAuditActivity;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/12/25
 * @description
 **/
public abstract class BaseVehicleAuditActivity extends BaseAuditActivity {

    @BindView(R.id.layout_audit_bottom_saveButton)
    Button mSaveButton;
    @BindView(R.id.layout_audit_bottom_nextButton)
    Button mNextButton;
    protected VehicleInfo mVehicleInfo;
    protected VehicleModel mVehicleModel;

    @Override
    public String getToolbarTitle() {
        String title = getTitle().toString();
        if (TextUtils.isEmpty(title)) {
            title = "车辆信息审核";
        }
        return title;
    }

    @Override
    public String getToolbarSubtitle() {
        return "返回车辆列表";
    }

    @Override
    public void onSubtitleClick() {
        super.onSubtitleClick();
        new MaterialDialog.Builder(this)
                .content("确认要退出当前操作吗？")
                .positiveText("是")
                .negativeText("否")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        openActivity(VehicleListActivity.class);
                    }
                })
                .show();
    }

    @Override
    public void init() {
        mVehicleModel = new VehicleModel();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            String plateNumber = bundle.getString(KeyValue.KEY_PLATE_NUMBER);
            int operate = bundle.getInt(KeyValue.KEY_OPERATE);
            showOperateView(operate);
            if (!TextUtils.isEmpty(plateNumber)) {
                requestVehicleInfo(plateNumber);
            } else {
                VehicleInfo info = bundle.getParcelable(KeyValue.KEY_VEHICLE_INFO);
                if (info != null) {
                    mVehicleInfo = info;
                    showVehicleInfo(info);
                }
            }

        }

    }

    protected void showOperateView(int operate) {
        if (operate == KeyValue.KEY_EDIT_OPERATE) {
            mSaveButton.setVisibility(View.VISIBLE);
        } else if (operate == KeyValue.KEY_AUDIT_OPERATE) {
            mSaveButton.setVisibility(View.GONE);
        }
    }

    private void requestVehicleInfo(String plateNumber) {

        Map<String, String> map = new HashMap<>();
        map.put("limit", "10");
        map.put("order", "desc");
        map.put("page", "1");
        map.put("sort", "create_time");
//        map.put("isaudit", mAuditStatus);
        map.put("vehiclenum", plateNumber);
        mVehicleModel.vehicleListModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<CommonDataListInfo<VehicleInfo>>>() {
            @Override
            public void onNext(CommonResponse<CommonDataListInfo<VehicleInfo>> commonResponse) {
                if (commonResponse.getCode() == 20000) {

                    CommonDataListInfo<VehicleInfo> commonDataListInfo = commonResponse.getData();
                    if (commonDataListInfo != null) {
                        List<VehicleInfo> list = commonDataListInfo.records;
                        if (list != null && list.size() > 0) {
                            mVehicleInfo = list.get(0);
                            showVehicleInfo(mVehicleInfo);
                        } else {
                        }
                    }

                } else {
                    showToast(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    private void requestVehicleInfoSave(boolean isNeedAudit, VehicleInfo info) {

        mVehicleModel.vehicleInfoSaveModel(this, info, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    if (isNeedAudit) {
                        requestVehicleAudit(info);
                    } else {
                        showToast(commonResponse.getMsg());
                    }
                    saveInfoSuccess();
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    public void requestVehicleInfoSaveAndAudit(VehicleInfo info) {

        mVehicleModel.vehicleInfoSaveAndAuditModel(this, info, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    openActivity(VehicleListActivity.class);
                    EventBusManage.refreshVehicleList();
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    private void requestVehicleAudit(VehicleInfo info) {
        Map<String, String> map = new HashMap<>();
        map.put("id", info.id);
        map.put("isaudit", "1");
        mVehicleModel.driverAuditModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    openActivity(VehicleListActivity.class);
                    EventBusManage.refreshVehicleList();
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    public abstract void showVehicleInfo(VehicleInfo info);

    public abstract VehicleInfo getSaveInfo();

    public abstract void nextAudit();

    public void saveInfoSuccess() {

    }

    @OnClick(R.id.layout_audit_bottom_saveButton)
    public void save() {
        saveInfo(false);
    }

    public void saveInfo(boolean isNeedAudit) {
        VehicleInfo info = getSaveInfo();
        if (info != null)
            requestVehicleInfoSave(isNeedAudit, info);
    }

    @OnClick(R.id.layout_audit_bottom_nextButton)
    public void next() {
        nextAudit();
    }

    @Override
    public String getPictureModule() {
        return "driverCar";
    }

    public void requestTypeList(int type, Map<String, String> map, boolean isDialog) {
        mVehicleModel.dictionaryModel(this, map, isDialog, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<DictionaryInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<DictionaryInfo>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    List<DictionaryInfo> list = commonResponse.getData();
                    if (null != list && list.size() > 0) {
                        showTypeList(type, list);
                    } else {
                        showToast("获取数据为空");
                    }
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    public void showTypeList(int type, List<DictionaryInfo> list) {
//        switch (type) {
//            case DictionaryType.TYPE_VEHICLE:
//                mVehicleDialog = new SelectDialog(this);
//                showSelectDialog(DictionaryType.TYPE_VEHICLE, "请选择车辆类型", mVehicleDialog, mVehicleTypeSettingItemView, list);
//                break;
//            case DictionaryType.TYPE_PLATE:
//                mPlateTypeDialog = new SelectDialog(this);
//                showSelectDialog(DictionaryType.TYPE_PLATE, "请选择车牌类型", mPlateTypeDialog, mPlateTypeSettingItemView, list);
//                break;
//            case DictionaryType.TYPE_PLATE_COLOR:
//                mPlateColorDialog = new SelectDialog(this);
//                showSelectDialog(DictionaryType.TYPE_PLATE_COLOR, "请选择车牌颜色", mPlateColorDialog, mPlateColorSettingItemView, list);
//                break;
//            case DictionaryType.TYPE_VEHICLE_ENERGY:
//                mVehicleEnergyTypeDialog = new SelectDialog(this);
//                showSelectDialog(DictionaryType.TYPE_VEHICLE_ENERGY, "请选择能源类型", mVehicleEnergyTypeDialog, mVehicleEnergyTypeSettingItemView, list);
//                break;
//        }

    }

    //车辆类型 车牌类型 车牌颜色 车辆能源类型 选择框
    protected void showSelectDialog(int type, String title, SelectDialog selectDialog, ClickItemView clickItemView, List<DictionaryInfo> list) {

        selectDialog.setTitle(title);

        selectDialog.setList(list);
        selectDialog.setOnSelectItemClickLister(new SelectDialog.OnSelectItemClickListener<com.fgs.common.interf.ISelectItemValue>() {
            @Override
            public void onItemClick(com.fgs.common.interf.ISelectItemValue iSelectItemValue, int position) {
                clickItemView.setContent(iSelectItemValue.getSelectItemText());
                setTypeInfo(type, iSelectItemValue);
            }
        });
        selectDialog.show();
    }

    /**
     * 设置选择的车辆类型 车牌类型 车牌颜色 车辆能源类型 的值
     *
     * @param type
     * @param iSelectItemValue
     */
    public void setTypeInfo(int type, com.fgs.common.interf.ISelectItemValue iSelectItemValue) {
//        switch (type) {
//            case DictionaryType.TYPE_VEHICLE:
//                mVehicleAuthInfo.vehicletype = iSelectItemValue.getSelectItemTextValue();
//                break;
//            case DictionaryType.TYPE_PLATE:
//                mVehicleAuthInfo.vehiclenumtype = iSelectItemValue.getSelectItemTextValue();
//                break;
//            case DictionaryType.TYPE_PLATE_COLOR:
//                mVehicleAuthInfo.vehiclenumcolortype = iSelectItemValue.getSelectItemTextValue();
//                break;
//            case DictionaryType.TYPE_VEHICLE_ENERGY:
//                mVehicleAuthInfo.vehicleenergytype = iSelectItemValue.getSelectItemTextValue();
//                break;
//        }
    }

    @Override
    public void finish() {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putParcelable(KeyValue.KEY_VEHICLE_INFO, mVehicleInfo);
        intent.putExtras(bundle);
        setResult(KeyValue.KEY_RESULT_CODE, intent);
        super.finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == KeyValue.KEY_RESULT_CODE) {
            Bundle bundle = data.getExtras();
            if (bundle != null && !bundle.isEmpty()) {
                VehicleInfo info = bundle.getParcelable(KeyValue.KEY_VEHICLE_INFO);
                if (info != null) {
                    mVehicleInfo = info;
                }
            }
        }
    }
}
